//
"use strict"
// const dbase = require("../dbase");
const mvc = require("cosjs.mvc")
const dbase = mvc.model.require("dbase");

class eggPool extends dbase.gzone.mongodb {
    constructor(sid, uid) {
        super(sid, 'eggPool', true);
        this.uid = uid;
        this.ObjectID = ObjectID.bind(this);
    }
    setOnInsert(id) {
        return { "id": id, "uid": this.uid };
    }
    uget(uid, fid) {
        let oid = ObjectID(uid, fid);
        let keys = ["uid", "itemList"];
        // let query  = this.query(oid);
        let query = { "_id": oid }
        let option = { "multi": this.isMulti(oid), "fields": this.fields(keys), "key": "uid" };
        // option['fields']["fid"] = 1;
        // option['fields']["_id"] = -1;
        return this.find(query, option);
    }
    //获取玩家道具列表
    mget(bag) {
        let query = { "uid": this.uid };
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        if (bag) {
            query["bag"] = bag;
        }
        return this.find(query, option);
    }


    mpage(page, bag) {

        let $page = page || 1;
        let $size = 500;
        let $sort = '_id';
        let $order = 'desc';
        let dbsort = {};
        if ($sort) {
            dbsort[$sort] = $order == 'desc' ? -1 : 1;
        }
        let query = { "uid": this.uid };
        if (bag) {
            query["bag"] = bag;
        }
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        return this.page(query, $page, $size, dbsort, option);

    }

    apply(uid, poolID, data) {
        // let time = Date.now();
        //甚至可以在这里初始化
        // let config = mvc.config.get("friend");
        // if(!config){
        //     return promise.callback("config_not_exist","friend");
        // }
        // let option = {"multi":false,"upsert":true};

        // //对方的好友列表
        // //关注模式需要提示对方，双向不需要确认直接成为好友，双向确认模式等待确认
        // let pv =1;
        // let pf = format_update.call(this,fid,uid,pv,time);
        // this.update(pf.query,pf.update,option);
        //保存

        let option = { "multi": false, "upsert": true };
        this.multi();
        let sf = format_update.call(this, uid, poolID, data);
        this.update(sf.query, sf.update, option);

        // this.multi();
        // let update = {"itemList":data};
        // let option = {"multi":false,"upsert":true};

        // this.update({"uid":uid},update,option);
        // if(config['apply'] > 0){
        //     this.update({"fid":uid,"val":-1},update,option);
        // }
        return this.save();

        // return this.save();
    }

}



module.exports = function (sid, uid) {
    return new eggPool(sid, uid);
};

// // 索引
// module.exports.indexes = [
//     [{"uid":1},{}]
// ]

function ObjectID(uid, eggPoolID) {
    return [uid, eggPoolID].join('-');
}

function format_update(uid, eggPoolID, data) {
    let query = { "_id": ObjectID(uid, eggPoolID) }
    let update = { "$set": { "uid": uid, "itemList": data } };
    return { "query": query, "update": update };
}
